package com.uchedao.seller.ui.user.login;

import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.JsonObject;
import com.uchedao.seller.R;
import com.uchedao.seller.manager.UserInfoManager;
import com.uchedao.seller.http.Api;
import com.uchedao.seller.http.HttpRequest;
import com.uchedao.seller.ui.baidu.BaseFragForBaidu;
import com.uchedao.seller.ui.main.IndexFragment;
import com.uchedao.seller.util.ViewUtil;
import com.uchedao.seller.widget.MyEditView;
import com.uchedao.seller.widget.TitleLayoutView;
import com.uchedao.ulibrary.fragment.FragmentMrg;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

/**
 * 密码登录
 * Created by zhengxizhen on 15/10/26.
 */
public class PassWordLoginFragment extends BaseFragForBaidu implements View.OnClickListener {

    private static BaseFragForBaidu mInstance = null;
    private String mPhone;
    private String password;

    /**
     * 获取单例
     */
    public static synchronized BaseFragForBaidu getInstance() {
        if (mInstance == null) {
            mInstance = new PassWordLoginFragment();
        }
        return mInstance;
    }

    private MyEditView mev_user_pwd, mev_user_phone;
    private TitleLayoutView titleLayoutView;

    @Override
    protected String getActivityTag() {
        return "PassWordLoginFragment";
    }

    @Override
    public int getViewLayoutId() {
        return R.layout.fragment_login_password;
    }

    @Override
    public void initListener() {
        findViewById(R.id.btn_pwd_login).setOnClickListener(this);
        findViewById(R.id.btn_sms_login).setOnClickListener(this);
        findViewById(R.id.btn_change_password).setOnClickListener(this);
    }

    @Override
    public void initData() {
        titleLayoutView.setData("帐号登录", this);
        if (TextUtils.isEmpty(UserInfoManager.getMOBILE())) {
            mev_user_phone.setETContent(UserInfoManager.getMOBILE());
        }
    }

    @Override
    public void initView() {
        titleLayoutView = (TitleLayoutView) findViewById(R.id.ll_title);

        mev_user_phone = (MyEditView) findViewById(R.id.mev_user_phone);
        mev_user_pwd = (MyEditView) findViewById(R.id.mev_user_pwd);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                FragmentMrg.toBack(mInstance);
                break;
            case R.id.btn_pwd_login:
                if (confirmUserInfo()) {
                    requestLogin();
                }
                break;
            case R.id.btn_sms_login:
                toFragment(SmsLoginFragment.getInstance(), true, true);
                break;
            case R.id.btn_change_password:
                toFragment(ChangePassWordFragment.getInstance(), true, true);
                break;
            default:
                break;
        }
    }

    /**
     * 确认用户信息是否合法
     */
    private boolean confirmUserInfo() {
        mPhone = mev_user_phone.getString();
        password = mev_user_pwd.getString();
        if (TextUtils.isEmpty(mPhone)
                || TextUtils.isEmpty(password)) {
            return false;
        }
        if (mPhone.length() < 11) {
            ViewUtil.showEditError(mev_user_phone.getEt_content(), getString(R.string.phone_is_error));
            return false;
        }
        return true;
    }

    /**
     * 请求登录接口
     */
    private void requestLogin() {
        // 获取验证码，并进入下一个页面
        Map<String, String> map = new HashMap<String, String>();
        map.put("mobile", mPhone);
        map.put("password", password);
        map.put("valid_code", "");
        map.put("app_name", "buy");
        map.put("registration_id", JPushInterface.getRegistrationID(getContext()));
        Request request = HttpRequest.getRequest(HttpRequest.POST, Api.Action.USER_LOGIN, map
                , new Response.Listener<JsonObject>() {
            @Override
            public void onResponse(JsonObject response) {
                //注册成功，并登录
                UserInfoManager.savePASS_PORT(response.get("passport").getAsString());
                UserInfoManager.saveSTATE(true);
                UserInfoManager.saveMOBILE(mPhone);
                UserInfoManager.savePASSWORD(password);
                UserInfoManager.saveUSERNAME(mPhone);
                showToast(getString(R.string.login_succes));
                //前往主页，并清空堆栈
                toFragment(IndexFragment.getInstance(), false, true);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), error.getMessage() + "", Toast.LENGTH_SHORT).show();
            }
        });
        addQueue(request);
    }
}
